﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using WebService.DataAccess;using WebService.BusinessEntities;
using WebService.DataAccess.Generic;
using System.Globalization;

namespace WebService.DataAccess
{
    /// <summary>
    /// A factory object that is responsible for taking a LoginAudit
    /// and generating the corresponding SQL to insert that
    /// LoginAudit into the database. It also includes a method
    /// to grab the returned ID from the call and updating the
    /// LoginAudit object with it.
    /// </summary>
    internal class LogoutAuditInsertFactory : IDbToBusinessEntityNameMapper, IInsertFactory<LoginAudit>
    {
        /// <summary>
        /// Creates the LoginAuditInsertFactory to build an insert statement for
        /// the given LoginAudit object.
        /// </summary>
        /// <param name="LoginAudit">New LoginAudit to insert into the database.</param>
        public LogoutAuditInsertFactory()
        {
        }

		#region IInsertFactory<LoginAudit> Members
		
        public DbCommand ConstructInsertCommand(Database db, LoginAudit loginAudit)
        {            
			DbCommand command = db.GetStoredProcCommand("dbo.INSERT_LOGOUT_AUDIT");
			
			db.AddInParameter(command, "logon_date", DbType.DateTime, loginAudit.logon_date);
			db.AddInParameter(command, "status", DbType.Boolean, loginAudit.status);
			db.AddInParameter(command, "user_id", DbType.Int32, loginAudit.user_id);
			
            return command;
        }

        public void SetNewID(Database db, DbCommand command, LoginAudit loginAudit)
        {
            //TODO: Provide set mapping
        }
        
        #endregion
        
		#region IDbToBusinessEntityNameMapper Members
        /// <summary>
        /// Maps a field name in the database (usually a parameter name for a stored proc)
        /// to the corresponding business entity property name.
        /// </summary>
        /// <remarks>This method is intended for error message handling, not for reflection.</remarks>
        /// <param name="dbParameter">Name of field/parameter that the database knows about.</param>
        /// <returns>Corresponding business entity field name.</returns>
        public string MapDbParameterToBusinessEntityProperty(string dbParameter)
        {
            switch (dbParameter)
            {
				case "logon_date":
					return "logon_date";
				case "status":
					return "status";
				case "user_id":
					return "user_id";
				default:
					throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, GenericResources.InvalidParameterName), dbParameter);
			}
        }
        #endregion        
    }

}

